// pages/mine/baby/index.js
var request = require("../../../utils/request.js");
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    babys:[],
    sexArr: ["未知", "男", "女"]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.init();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 添加宝宝
  addBabys:function(){
    var that = this;
    var data = {
      ID:0,
      Name:'',
      Gender:'0',
      Birth:'2000-01-01',
      Hobby:'',
      labels:that.data.labels
    }
    var babys = that.data.babys;
    var labels = [];
    

    babys.push(data);

    that.setData({
      babys: babys
    })
  },
  //删除宝宝
  delBabys:function(e){
    var that = this;
    var index = e.currentTarget.dataset.index;
    var babys = that.data.babys;
    
    request.request("api/Customer/DeleteChild", { ID: babys[index].ID},'POST').then(function(res){
      console.log(res);
      if(res.data.status == 1){
        babys.splice(index, 1)
        that.setData({
          babys: babys
        })
      }
    })
  },
  //修改值
  setValue:function(e){
    var that = this;
    var index = e.currentTarget.dataset.index;
    var value = e.detail.value;
    var field = e.currentTarget.dataset.field;
    var str = "babys["+index+"]."+field
    that.setData({
      [str]:value
    })
  },
  //保存值
  saveBabys:function(e){
    var that = this;
    var babys = that.data.babys;
    console.log(babys);
    for(var i in babys){
      var data = babys[i];
      data['Remark'] = ""+Math.round(new Date(data['Birth']) );
      data['CustomerID'] = app.globalData.userInfo['ID'];

      var labels = JSON.parse(JSON.stringify(that.data.babys[i]['labels']));
      var hobby = '';
      for (var j in labels) {
        if (labels[j]['show']) {
          var label = labels[j]['ID'] + ","
          hobby += label
        }
      }
      hobby = hobby.substring(0, hobby.length - 1);
      console.log("hobby:"+i+":" +hobby);
      data['Hobby'] = hobby;

      delete(data['Birth'])
      request.request("api/Customer/EditChild",data,"POST",1).then(
        function (res) {
          console.log(res)
        }
      )
    }
    wx.showToast({
      title: '保存完成',
    })
  },
  //获取宝宝
  init:function(){
    var that = this;
  
    request.request("api/CourseLabel/GetCourseLabels").then(function (res) {
      if(res.data.status == 1){

        var data = res.data.data;

        console.log("data", data);

        let labels = JSON.parse(JSON.stringify(data));

        console.log("labels",labels);
        
        that.setData({
          labels: data
        })

        that.getBabys(labels);
      }
    })
  },

  getBabys: function (labels){

    

    console.log("getBabys",labels);

    var that = this;
    request.request("api/Customer/GetMyChildren", { id: app.globalData.userInfo['ID'] }).then(function (res) {
      console.log(res);
      if (res.data.status == 1) {

        var data = res.data.data;
        for (var i in data) {

          var myLabels = JSON.parse(JSON.stringify(labels));

          data[i]['Birth'] = that.timeToStr(data[i]['Birth'])
          
          var hobby = data[i]['Hobby'].split(',')
          console.log(hobby)
          for (var l in myLabels){
            if (hobby.indexOf(myLabels[l]['ID'] + "") > -1) {
              myLabels[l]['show'] = true
            }
            else {
              myLabels[l]['show'] = false
            }
            data[i]['labels'] = myLabels;
          }

        }
        console.log(data);
        that.setData({
          babys: data
        })
      }
    })
  },
  
  formatDate:function(dt){
    var year = dt.getFullYear();
    var month = dt.getMonth() + 1;
    var date = dt.getDate();
    var hour = dt.getHours();
    var minute = dt.getMinutes();
    var second = dt.getSeconds();
    return year + '-' + month + '-' + date; 
  },


  timeToStr:function(time)
  {
    console.log(time)
    var t = time.slice(6, 19)
    var NewDtime = new Date(parseInt(t));
    return this.formatDate(NewDtime); 
  },

  getMyHobby:function(e){
    var that = this;
    var pid = e.currentTarget.dataset.index;
    console.log(pid);
    var index = e.currentTarget.dataset.sub_index;

    var labels = JSON.parse(JSON.stringify(that.data.babys[pid]['labels']));
    console.log(labels);
    labels[index]['show'] = labels[index]['show'] ? false : true;
    
    var str = "babys[" + pid + "].labels";
    console.log(str, labels);
    that.setData({
      [str]: labels
    })

    console.log(that.data.babys);

  }

})